package com.demo.zhou.sniper.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.application.MyApplication;
import com.demo.zhou.sniper.gen.DaoSession;
import com.demo.zhou.sniper.gen.QunbeanDao;
import com.demo.zhou.sniper.gen.ZubeanDao;
import com.demo.zhou.sniper.module.Qunbean;
import com.demo.zhou.sniper.module.Zubean;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表，显示所有群信息
 * Created by zhou on 2017/6/18.
 */

public class AllqunActivity extends BaseActivity {
    private ListView allqun_lv_qunid;
    List<Qunbean> qunAll;
    List<String> qunNameAll;
    DaoSession daoSession;
    QunbeanDao qunbeanDao;
    ZubeanDao zubeanDao;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("群查看");
        setRightBtnVisible(false);
        into();
        onClick();
    }

    public void into() {
        allqun_lv_qunid = (ListView) findViewById(R.id.allqun_lv_qunid);
        daoSession = ((MyApplication) getApplication()).getDaoSession();
        qunbeanDao = daoSession.getQunbeanDao();
        qunAll = qunbeanDao.loadAll();
        qunNameAll = queryQun(qunAll);
        adapter = new ArrayAdapter<String>(this,
                R.layout.item_allqun_list,
                R.id.tv_item,
                qunNameAll);
        allqun_lv_qunid.setAdapter(adapter);

    }

    public void onClick() {
        // 添加长按点击,得到点中的index，即参数arg2
        allqun_lv_qunid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           final int arg2, long arg3) {
                //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(AllqunActivity.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (deleteData(qunAll.get(arg2).getId())) {
                            // 重新获取数据库数据
                            qunAll.clear();
                            qunAll.addAll(qunbeanDao.loadAll());
                            // 删除列表相应数据
                            if (qunNameAll.remove(arg2) != null) {
                                System.out.println("success");
                            } else {
                                System.out.println("failed");
                            }
                            adapter.notifyDataSetChanged();
                            Toast.makeText(AllqunActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(AllqunActivity.this, "删除失败，请重试！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
                return true;
            }
        });
        allqun_lv_qunid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击进入
                Intent intent = new Intent(AllqunActivity.this, ZudetailsActivity.class);
                Bundle data = new Bundle();
                data.putSerializable("qunId", qunAll.get(position).getId());
                intent.putExtras(data);
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    protected int getContentView() {
        return R.layout.activity_allqun;
    }

    public List<String> queryQun(List<Qunbean> qunList) {
        //--执行查询操作
        List<String> qunName = new ArrayList<String>();
        for (int i = 0; i < qunList.size(); i++) {
            qunName.add(qunList.get(i).getQun_name());
        }
        return qunName;
    }

    public boolean deleteData(Long id) {
        try {
            zubeanDao = daoSession.getZubeanDao();
            List<Zubean> zuAll = qunbeanDao.load(id).getZu();
            for (int i = 0; i < zuAll.size(); i++) {

                zubeanDao.deleteByKey(zuAll.get(i).getId());
                qunbeanDao.load(id).resetZu();
            }
            // 删除数据库数据
            qunbeanDao.deleteByKey(id);
        } catch (Throwable t) {
            t.printStackTrace();
            return false;
        }

        return true;
    }

}
